#include <stdio.h>
#include <math.h>

struct rational
{
    int num;
    int deno;
};

// 求解两个数的最大公约数
int myGCD(int a, int b)
{	
    if(a == 0 || b ==0)
    {
        return 1;
    }
	a = fabs(a);
	b = fabs(b);
	if(a < b)
	{
		int change = a;
		a = b;
		b = change;
	}
	int result;
	if(!(a%b))
	{
		result = b;
		// printf("1.a = %d and b = %d.\n", a, b);
	}		
	else
	{
		result = myGCD(b,a%b); 
		// printf("2.a = %d and b = %d.\n", a, b);
	}
		
	return result;
}

// 求两个数的最小公约数
int LCM(int a, int b)
{
    return fabs(a*b)/myGCD(a,b);
}

// 构造有理数
struct rational make_rational(int num, int deno)
{
    struct rational r;
    int gcd;
    if((gcd = myGCD(num, deno)) == 1)
    {
        r.num = num;
        r.deno = deno;
        //return r;
    }
    else
    {
        r.num = num / gcd;
        r.deno = deno /gcd;
        //return r;
    }

    // 让负号体现在分子上
    if(r.num * r.deno < 0.0)
    {
        r.num = -fabs(r.num);
        r.deno = fabs(r.deno);
    }
    return r;
}


struct rational tongfen(struct rational a, int newDeno)
{
    int mul = fabs(newDeno / a.deno);
    a.num *= mul;
    a.deno *= mul;
    return a;
}
// 析构有理数
// int get_num(struct rational a)
// {
//     return a.num;
// }
// int get_deno(struct rational a)
// {
//     return a.deno;
// }

// 加减乘除
struct rational add_rational(struct rational a, struct rational b)
{
    int num, deno;
    struct rational r;
    int mul = LCM(a.deno, b.deno);
    a = tongfen(a, mul);
    b = tongfen(b, mul);
    num = a.num + b.num;
    //deno = a.deno + b.deno;
    r = make_rational(num, a.deno);
    return r;

}

struct rational sub_rational(struct rational a, struct rational b)
{
    int num, deno;
    struct rational r;
    int mul = LCM(a.deno, b.deno);
    a = tongfen(a, mul);
    b = tongfen(b, mul);
    num = a.num - b.num;
    //deno = a.deno - b.deno;
    r = make_rational(num, a.deno);
    return r;
}

struct rational mul_rational(struct rational a, struct rational b)
{
    int num, deno;
    struct rational r;
    num = a.num * b.num;
    deno = a.deno * b.deno;
    r = make_rational(num, deno);
    return r;
}

struct rational div_rational(struct rational a, struct rational b)
{
    int num, deno;
    struct rational r;
    num = a.num * b.deno;
    deno = a.deno * b.num;
    r = make_rational(num, deno);
    return r;
}

void print_rational(struct rational r)
{
    struct rational a = make_rational(r.num, r.deno);
    r.num = a.num;
    r.deno = a.deno;    
    printf("%d/%d.\n",r.num, r.deno);
}

int main(void)
{
	struct rational a = make_rational(1, 8); /* a=1/8 */
	struct rational b = make_rational(-1, 8); /* b=-1/8 */
	print_rational(add_rational(a, b));
	print_rational(sub_rational(a, b));
	print_rational(mul_rational(a, b));
	print_rational(div_rational(a, b));

	return 0;
}
